<%@ page import="com.beerandjesus.calc.bean.*" %>
<%@page contentType="UTF-8" %>

<jsp:useBean id="calcBean" scope="request"
             class="com.beerandjesus.calc.bean.CalculatorBean"/>
<%
    String names = request.getParameter("names");
    String times = request.getParameter("times");
    String both = request.getParameter("both");
    String output = null;
    if (times != null && !times.trim().equals("")
            && names != null && !names.trim().equals("")) {
        calcBean.setTrackNames(names);
        calcBean.setTrackTimes(times);
        output = calcBean.prepareOutput();
    } else if (both != null && !both.trim().equals("")) {
        calcBean.setDebug(true);
        calcBean.setBoth(both);
        output = calcBean.prepareOutputForBoth();
    } else {
        names = "";
        times = "";
        output = "";
        both="";
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
    <title>Calculator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
        function switchInputs() {
            var singleField = document.getElementById("singleInput");
            var doubleField = document.getElementById("splitInput");
            var switchLink = document.getElementById("switchLink");
            if (singleField.style.display == 'table-row') {
                singleField.style.display = 'none';
                doubleField.style.display = 'table-row';
                switchLink.innerHTML = "&lt;&lt; Combine Input Fields";
            } else {
                singleField.style.display = 'table-row';
                doubleField.style.display = 'none';
                switchLink.innerHTML = "&lt;&lt; Split Input Fields";
            }
        }
    </script>
    <link rel="stylesheet" href="/calc/style.css" type="text/css" />
</head>
<body>
<h2>Calculator</h2>
<div class="hr">
</div>
<form action="/calc/index.jsp" method="post">
    <a id="switchLink" href="javascript:switchInputs();">&lt;&lt; Split Input Fields</a>
    <table>
        <tr id="singleInput" style="display: table-row;">
            <td colspan="2">
                Tracks and Times:<br />
                <textarea rows="15" cols="65" name="both"><%=both%></textarea>
            </td>
        </tr>
        <tr id="splitInput" style="display: none;">
            <td>
                Track names:<br/>
                <textarea rows="15" cols="45" name="names"><%=names%>
                </textarea>
            </td>
            <td>
                Track times:<br/>
                <textarea rows="15" cols="10" name="times"><%=times%>
                </textarea>
            </td>
        </tr>
    </table>
    <input type="submit" value="SUBMIT"/>
</form>
<div class="hr">
</div>
<div class="output">
    <%=output%>
</div>

</body>
</html>
